const { HttpException }  = require('../core/httpException');

const catchError = async (ctx, next) => {
  try {
    await next();
  } catch (e) {
    if (e instanceof HttpException) {
      ctx.body = {
        error_code: e.errorCode,
        message: e.msg,
        request: `${ctx.method} ${ctx.path}`,
      };
      ctx.status = e.code;

    }
  }
};

module.exports = catchError;